/**   
 * @Title: ServiceTaskQueue.java 
 * @Package cn.edu.cdtu.healthclient.task 
 * @Description: 后台任务执行队列
 * @author 
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthclient.task;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/** 
 * @ClassName: ServiceTaskQueue 
 * @Description: 后台任务执行队列，与前台不同的是设置的任务是常驻的，可以多次启动,
 * 				且每次启动的是所设置任务的副本
 * @author 
 * @date 
 *  
 */
public class ServiceTaskQueue extends Thread {
	
	/*
	 * 任务队列启动标识
	 */
	private boolean isRunning = false;
	/*
	 * 队列终止标识
	 */
	private volatile boolean exit = false;
	/**
	 * 
	 * @ClassName: TaskSet 
	 * @Description: TODO
	 * @author 
	 * @date 	 *
	 */
	private class TaskSet{
		//任务名称
		public String taskName;
		//任务（及一个线程.）
		public Thread task;
		/** 
		 * @Title: TaskSet
		 * @Description: 任务设置，为任务命名和赋值。
		 * @throws 
		 *
		 * @param taskName
		 * @param task
		 */
		public TaskSet(String taskName, Thread task) {
			this.taskName = taskName;
			this.task = task;
		}
		
	}
	private volatile List<TaskSet> taskList = new LinkedList<TaskSet>();
	private volatile Queue<Thread> excuteList = new LinkedList<Thread>();
	
	/**
	 * @return the isRunning
	 */
	public boolean isRunning() {
		return isRunning;
	}
	/**
	 * 
	 * @Title: shutdownTaskQueue 
	 * @Description: 终止任务队列
	 * @return void
	 * @throws 
	 *
	 */
	public void shutdownTaskQueue(){
		synchronized (excuteList) {
			exit = true;
			if(isRunning){
				excuteList.notifyAll();
			}
		}
	}
	/**
	 * 
	 * @Title: setTask 
	 * @Description: TODO
	 * @return void
	 * @throws 
	 *
	 * @param taskName
	 * @param task
	 */
	public void setTask(String taskName, Thread task){
		boolean isExisted = false;
		synchronized (taskList) {
			for (TaskSet taskSet : taskList) {
				if(taskSet.taskName.equals(taskName)){
					taskSet.task = task;
					break;
				}
			}
			if(!isExisted){
				taskList.add(new TaskSet(taskName, task));
			}
			if(isRunning){
				taskList.notifyAll();
			}
		}
	}
	/**
	 * 
	 * @Title: startTask 
	 * @Description: 根据提供的任务名称开始一个任务。
	 * @return void
	 * @throws 
	 *
	 * @param taskName
	 */
	public void startTask(String taskName){
		synchronized (taskList) {
			//在任务队列中查找指定的任务。
			for (TaskSet taskSet : taskList) {
				if(taskSet.taskName.equals(taskName)){
					synchronized (excuteList) {
						excuteList.offer(new Thread(taskSet.task));
						if(isRunning){
							excuteList.notifyAll();
						}
					}
					break;
				}
			}
			if(isRunning){
				taskList.notifyAll();
			}
		}
	}
	
	/* (non-Javadoc)
	 * <p>Title: run</p> 
	 * <p>Description: 运行任务。
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		isRunning = true;
		while (!exit) {
			synchronized (excuteList) {
				Thread task = null;
				if(excuteList.size() < 1){
					try {
						excuteList.wait();
						task = excuteList.poll();
						if(exit){
							break;
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				if(task == null){
					task = excuteList.poll();
				}
				task.start();
			}
		}
	}
	/* (non-Javadoc)
	 * <p>Title: start</p> 
	 * <p>Description: </p>  
	 * @see java.lang.Thread#start()
	 */
	@Override
	public synchronized void start() {
		// TODO Auto-generated method stub
		exit = false;
		super.start();
	}

}
